<template>
  <div class="talent-container">
    <div class="talent-header">
      <h1>我们的艺人</h1>
      <p>汇聚行业精英，打造明日之星</p>
    </div>

    <div class="talent-content">
      <el-tabs v-model="activeCategory" class="talent-tabs" @tab-click="handleCategoryChange">
        <el-tab-pane label="全部艺人" name="all"></el-tab-pane>
        <el-tab-pane label="演员" name="actor"></el-tab-pane>
        <el-tab-pane label="导演" name="director"></el-tab-pane>
        <el-tab-pane label="编剧" name="writer"></el-tab-pane>
      </el-tabs>

      <div class="talent-search">
        <el-input
            v-model="searchQuery"
            placeholder="搜索艺人姓名或作品"
            clearable
            @clear="handleSearchClear"
            @keyup.enter="handleSearch"
            style="height: 40px; line-height: 40px;"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" @click="handleSearch" style="height: 40px;">
          搜索
        </el-button>
      </div>

      <el-row :gutter="30" class="talent-list">
        <el-col
            v-for="(talent, index) in talentsData"
            :key="talent.id || index"
            :xs="24" :sm="12" :md="8" :lg="6"
        >
          <div class="talent-card" @click="handleTalentDetail(talent)">
            <div class="talent-avatar">
              <img :src="talent.avatar" :alt="talent.name" />
              <div class="talent-badge" v-if="talent.isFeatured">
                <el-icon><StarFilled /></el-icon>
              </div>
            </div>
            <div class="talent-info">
              <h3>{{ talent.name }}</h3>
              <p class="talent-title">{{ talent.title }}</p>
              <div class="talent-stats">
                <span>
                  <el-icon><VideoCamera /></el-icon>
                  {{ talent.worksCount }}部作品
                </span>
                <span>
                  <el-icon><Trophy /></el-icon>
                  {{ talent.awardsCount }}个奖项
                </span>
              </div>
              <div class="talent-tags">
                <el-tag
                    v-for="(tag, i) in talent.tags"
                    :key="i"
                    size="small"
                    :type="tagStyleMap[tag] || ''"
                >
                  {{ tagLabelMap[tag] || tag }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>

      <div class="talent-pagination">
        <el-pagination
            background
            layout="prev, pager, next"
            :total="totalTalents"
            :page-size="pageSize"
            v-model:current-page="currentPage"
            @current-change="handlePageChange"
        />
      </div>
    </div>

    <!-- 艺人详情对话框 -->
    <el-dialog v-model="detailVisible" :title="currentTalent?.name" width="70%">
      <div class="talent-detail" v-if="currentTalent">
        <div class="detail-header">
          <div class="detail-avatar">
            <img :src="currentTalent.avatar" :alt="currentTalent.name" />
          </div>
          <div class="detail-basic">
            <h2>{{ currentTalent.name }}</h2>
            <p class="detail-title">{{ currentTalent.title }}</p>
            <p class="detail-bio">{{ currentTalent.bio }}</p>
            <div class="detail-meta">
              <p><strong>出生日期：</strong>{{ currentTalent.birthday }}</p>
              <p><strong>身高：</strong>{{ currentTalent.height }}cm</p>
              <p><strong>代表作品：</strong>{{ currentTalent.representativeWorks?.join('、') }}</p>
            </div>
          </div>
        </div>

        <el-divider />

        <div class="detail-works" v-if="currentTalent.works?.length">
          <h3>作品集</h3>
          <el-scrollbar>
            <div class="works-list">
              <div
                  v-for="(work, i) in currentTalent.works"
                  :key="i"
                  class="work-item"
              >
                <img :src="work.poster" :alt="work.title" />
                <p>{{ work.title }}</p>
                <span>{{ work.year }}</span>
              </div>
            </div>
          </el-scrollbar>
        </div>

        <el-divider />

        <div class="detail-awards" v-if="currentTalent.awards?.length">
          <h3>获奖记录</h3>
          <el-timeline>
            <el-timeline-item
                v-for="(award, i) in currentTalent.awards"
                :key="i"
                :timestamp="award.year"
            >
              <p><strong>{{ award.name }}</strong> - {{ award.work }} ({{ award.category }})</p>
            </el-timeline-item>
          </el-timeline>
        </div>
      </div>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleContactTalent">商务合作</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import {
  Search,
  StarFilled,
  VideoCamera,
  Trophy
} from '@element-plus/icons-vue'
import { getArtistsInfo } from '@/api/artists'
import { dictType } from '@/api/dict'

const activeCategory = ref('all')
const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = 12
const detailVisible = ref(false)
const currentTalent = ref(null)

const talentsData = ref([])
const totalTalents = ref(0)

const tagLabelMap = ref({})
const tagStyleMap = {
  '0': 'success',
  '1': 'info',
  '2': 'warning',
  '3': 'danger',
  '4': 'primary',
  '5': ''
}

const fetchTagDict = async () => {
  try {
    const res = await dictType('jssl_artists_tag')
    const dictList = res.data.data || []
    tagLabelMap.value = dictList.reduce((map, item) => {
      map[item.dictValue] = item.dictLabel
      return map
    }, {})
  } catch (err) {
    console.error('获取艺人标签字典失败', err)
  }
}

const fetchTalents = async () => {
  const params = {
    pageNum: currentPage.value,
    pageSize,
    category: activeCategory.value !== 'all' ? activeCategory.value : undefined,
    keyword: searchQuery.value || undefined
  }
  try {
    const res = await getArtistsInfo(params)
    talentsData.value = res.data.rows || []
    totalTalents.value = res.data.total || 0
  } catch (err) {
    console.error('获取艺人数据失败', err)
  }
}

const handleSearch = () => {
  currentPage.value = 1
  fetchTalents()
}

const handleSearchClear = () => {
  searchQuery.value = ''
  currentPage.value = 1
  fetchTalents()
}

const handlePageChange = (page) => {
  currentPage.value = page
  fetchTalents()
}

const handleTalentDetail = (talent) => {
  currentTalent.value = talent
  detailVisible.value = true
}

const handleContactTalent = () => {
  detailVisible.value = false
}

watch(activeCategory, () => {
  currentPage.value = 1
  fetchTalents()
})

onMounted(() => {
  fetchTagDict()
  fetchTalents()
})
</script>





<style scoped>
.talent-container {
  padding: 80px 0;
  background-color: #f9f9f9;
}

.talent-header {
  text-align: center;
  margin-bottom: 60px;
}

.talent-header h1 {
  font-size: 36px;
  color: #333;
  margin-bottom: 20px;
}

.talent-header p {
  font-size: 18px;
  color: #666;
}

.talent-content {
  width: 90%;
  margin: 0 auto;
}

.talent-tabs {
  margin-bottom: 30px;
}

.talent-search {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 20px;
}

/* 或通过class更精确控制 */
.talent-search :deep(.el-input__wrapper) {
  height: 40px;
  padding: 0 15px;
}

.talent-search :deep(.el-input__inner) {
  height: 100%;
  line-height: 40px;
}

.talent-search .el-input {
  width: 250px;
}

.talent-list {
  margin-bottom: 40px;
}

.talent-card {
  background-color: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
  margin-bottom: 30px;
}

.talent-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.talent-avatar {
  position: relative;
  height: 0;
  padding-bottom: 120%;
  overflow: hidden;
}

.talent-avatar img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: all 0.3s ease;
}

.talent-card:hover .talent-avatar img {
  transform: scale(1.05);
}

.talent-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  background-color: #ffc107;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.talent-info {
  padding: 20px;
}

.talent-info h3 {
  font-size: 18px;
  margin-bottom: 5px;
  color: #333;
}

.talent-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
}

.talent-stats {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 12px;
  color: #888;
}

.talent-stats .el-icon {
  margin-right: 5px;
}

.talent-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.talent-pagination {
  margin-top: 20px;
  text-align: center;
}

.talent-detail {
  padding: 0 20px;
}

.detail-header {
  display: flex;
  gap: 30px;
  margin-bottom: 20px;
}

.detail-avatar {
  flex: 0 0 200px;
}

.detail-avatar img {
  width: 100%;
  border-radius: 8px;
}

.detail-basic {
  flex: 1;
}

.detail-basic h2 {
  font-size: 24px;
  margin-bottom: 5px;
  color: #333;
}

.detail-title {
  font-size: 16px;
  color: #666;
  margin-bottom: 15px;
}

.detail-bio {
  font-size: 15px;
  line-height: 1.6;
  color: #555;
  margin-bottom: 20px;
}

.detail-meta p {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.detail-works h3,
.detail-awards h3 {
  font-size: 20px;
  margin-bottom: 15px;
  color: #333;
}

.works-list {
  display: flex;
  gap: 15px;
  padding: 10px 0;
}

.work-item {
  flex: 0 0 120px;
  text-align: center;
  cursor: pointer;
}

.work-item img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 5px;
  transition: all 0.3s ease;
}

.work-item:hover img {
  transform: scale(1.05);
}

.work-item p {
  font-size: 13px;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.work-item span {
  font-size: 12px;
  color: #888;
}

@media (max-width: 768px) {
  .detail-header {
    flex-direction: column;
  }

  .detail-avatar {
    flex: 0 0 auto;
    margin-bottom: 20px;
  }

  .talent-card {
    margin-bottom: 20px;
  }
  :deep(.el-dialog) {
    width: 100% !important;  /* 移动端宽度调整为100% */
    margin: 20px auto !important;  /* 居中显示 */
  }
}
</style>
